<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>疫情管理</el-breadcrumb-item>
      <el-breadcrumb-item>区域消毒管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="4">
          <el-input placeholder="请输入单号" v-model="queryInfo.disinfectId" clearable @clear="getDisinfect">
            <el-button slot="append" icon="el-icon-search" @click="getDisinfect"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3" style="padding-left:20px">
          <el-cascader v-model="queryInfo.community" :options="cityData" :props="{ expandTrigger: 'hover', emitPath:false,checkStrictly:true }" @change="getDisinfect"></el-cascader>
        </el-col>
        <el-col :span="4"  style="padding-left:20px">
          <el-date-picker v-model="queryInfo.queryTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日" @change="getDisinfect">
          </el-date-picker>
        </el-col>
        <el-col :span="4" style="padding-left:20px" v-if="this.myidentity==='1'">
          <el-button type="primary" @click="addDialogVisible=true">添加打卡记录</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="disinfectList" border="">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="消毒单号" prop="disinfectId"></el-table-column>
        <el-table-column label="消毒情况" prop="disinfectDes"></el-table-column>
        <el-table-column label="消毒次数" prop="disinfectNum"></el-table-column>
        <el-table-column label="社区" prop="community"></el-table-column>
        <el-table-column label="详细地址" prop="address"></el-table-column>
        <el-table-column label="负责人" prop="leader"></el-table-column>
        <el-table-column label="负责人电话" prop="phone"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{row}">
            <span>{{row.createTime | dataFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间">
          <template slot-scope="{row}">
            <span>{{row.updateTime | dataFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" v-if="this.myidentity==='1'">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="ShowEdit(row.disinfectId)">修改</el-button>
            <el-button type="warning" size="mini" @click="removeById(row.disinfectId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 30, 50]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align:center" background>
      </el-pagination>
    </el-card>

    <!-- 修改 -->
    <el-dialog title="提示" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm" :rules="rules" ref="editFormRef" label-width="100px">
        <el-form-item label="单号" prop="disinfectId">
          <el-input v-model="editForm.disinfectId" disabled></el-input>
        </el-form-item>
        <el-form-item label="消毒情况" prop="disinfectDes">
          <el-input v-model="editForm.disinfectDes"></el-input>
        </el-form-item>
        <el-form-item label="消毒次数" prop="disinfectNum">
          <el-input v-model="editForm.disinfectNum"></el-input>
        </el-form-item>
        <el-form-item label="分配社区" prop="community">
          <el-cascader v-model="editForm.community" :options="cityData" :props="{ expandTrigger: 'hover', emitPath:false,checkStrictly:true }" @change="getDisinfect"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="editForm.leader"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="editDisinfect">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加商品对话框 -->
    <el-dialog title="提示" :visible.sync="addDialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="addForm" :rules="rules" ref="addFormRef" label-width="100px">
        <el-form-item label="消毒情况" prop="disinfectDes">
          <el-input v-model="addForm.disinfectDes"></el-input>
        </el-form-item>
        <el-form-item label="消毒次数" prop="disinfectNum">
          <el-input v-model="addForm.disinfectNum"></el-input>
        </el-form-item>
        <el-form-item label="消毒社区" prop="community">
          <el-cascader v-model="addForm.community" :options="cityData" :props="{ expandTrigger: 'hover', emitPath:false,checkStrictly:true }"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="addForm.address"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="addForm.leader"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addDisinfect">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/js/cityData.js'
export default {
  data () {
    return {
      myidentity: window.sessionStorage.getItem('identity'),
      queryInfo: {
        disinfectId: '',
        pageNum: 1,
        pageSize: 5,
        community: '',
        queryTime: ''
      },
      cityData,
      total: 0,
      disinfectList: [],
      editForm: {},
      editDialogVisible: false,
      rules: {
        leader: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        disinfectNum: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        community: [{ required: true, message: '请输入工作社区', trigger: 'blur' }],
        address: [{ required: true, message: '请输入工作地址', trigger: 'blur' }],
        disinfectDes: [{ required: true, message: '请输入消毒情况', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
      },
      addDialogVisible: false,
      addForm: {}
    }
  },
  created () {
    this.getDisinfect()
  },
  methods: {
    async getDisinfect () {
      // const mytime = this.dayjs(this.queryInfo.updateTime, 'MM-DD-YYYY')
      // this.queryInfo.updateTime = new Date(this.queryInfo.updateTime)
      // console.log(mytime)
      if (this.queryInfo.queryTime === null) this.queryInfo.queryTime = ''
      const { data: res } = await this.$http.get('disinfect/getDisinfect', { params: this.queryInfo })
      this.disinfectList = res.data.records
      this.total = res.data.total
    },
    // 分页器
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getDisinfect()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pageNum = newSize
      this.getDisinfect()
    },
    // 展示修改
    async ShowEdit (id) {
      const { data: res } = await this.$http.get(`disinfect/getDisinfectById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('修改信息失败')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 删除
    async removeById (id) {
      const confirm = await this.$confirm('此操作将永久删除该消毒情况, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        console.log(err)
      })
      if (confirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`disinfect/deleteDisinfectById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除物资失败')
      }
      this.$message.success('删除物资成功')
      this.getDisinfect()
    },
    // 修改
    async editDisinfect () {
      const { data: res } = await this.$http.put(`disinfect/editDisinfectById/${this.editForm.disinfectId}`, this.editForm)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      // console.log(res)
      this.$message.success('修改成功')
      this.editDialogVisible = false
      this.getDisinfect()
    },
    // 添加
    async addDisinfect () {
      const { data: res } = await this.$http.post('disinfect/addDisinfect', this.addForm)
      // console.log(res)
      if (res.meta.status !== 200) {
        this.addDialogVisible = false
        return this.$message.error('添加失败')
      }
      // console.log(res)
      this.$message.success('成功')
      this.addDialogVisible = false
      this.getDisinfect()
    },
    // 清空添加对话框
    resetAddForm () {
      this.$refs.addFormRef.resetFields()
      this.addDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
